package iwpl.project.servlet;

import java.awt.List;
import java.io.IOException;
import java.io.PrintWriter;



import javax.management.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import iwpl.project.dao.*;
import iwpl.project.entities.*;
import iwpl.project.entitymanagerhelper.*;
import iwpl.project.interfaces.*;

public class Control_RoleDetail extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Control_RoleDetail() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doDelete method of the servlet. <br>
	 *
	 * This method is called when a HTTP delete request is received.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doDelete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	
	
//	public void Call()
//		{
//			String where="";
//			
//			if(cboSearch)
//			{
//				where = cboSearch.value  +" like " + txtvalue ;
//			}
//			
//			if(where != "")
//				where = "where" + where; 
//		}
	
	public static void ExecuteControl(String Control, Forms acc)
	{
		//TODO add code here
		FormsDAO dao = new FormsDAO();
		
		if(Control == "Save")
		{
			EntityManagerHelper.beginTransaction();
			dao.save(acc);
			EntityManagerHelper.commit();
		}
		
		if(Control == "Update")
		{
			EntityManagerHelper.beginTransaction();
			dao.update(acc);
			EntityManagerHelper.commit();
		}
		
		if(Control == "Delete")
		{
			EntityManagerHelper.beginTransaction();
			dao.delete(acc);
			EntityManagerHelper.commit();
		}
	}
	public void doProcess(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		RoleDetails Rld=new RoleDetails();
		RoleDetailsDAO dao=new RoleDetailsDAO();
		
		int id=0;
		try {
			String action=request.getParameter("action");
		
			String sname=request.getParameter("txtFormName");
			
			
			
			
			
			
			
			if(action.equals("Delete"))
			{
				if(request.getParameter("id")!=null)
				{
					id= Integer.parseInt(request.getParameter("id").toString());
					Rld.setId(id);
					
					
					EntityManagerHelper.beginTransaction();
					dao.delete(Rld);
					EntityManagerHelper.commit();
					
					
					response.sendRedirect("../Manage_RoleDetails.jsp");
					
				}
			}
			
			
			else
			{
				response.sendRedirect("../Manage_RoleDetails.jsp");
			}
				
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.print(e.getMessage());
		}
		
		
		
	}
		
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doProcess(request, response);
	/*	response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doProcess(request, response);
		/*response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
